package com.enthusiasm.view;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.MotionEvent;

import com.enthusiasm.constants.AbstractSurfaceView;
import com.enthusiasm.constants.BitmapManager;
import com.enthusiasm.constants.SoundManager;
import com.enthusiasm.engine.MyButton;
import com.enthusiasm.engine.MyRect;
import com.enthusiasm.frame.GameCollectActivity;
import com.enthusiasm.frame.GameModeActivity;
import com.enthusiasm.frame.GameWaveActivity;

public class GameCollectView extends AbstractSurfaceView{

	private Bitmap background;
	private ArrayList<Bitmap> bmpCollect_lock;
	private ArrayList<Bitmap> bmpCollect_unlock;
	private Rect bgRect;
	private ArrayList<MyRect> collectRect;
	private MyRect backRect;
	private MyRect questionRect;
	private MyRect noticeRect;
	private ArrayList<MyButton> collectButton;
	private MyButton back;
	private MyButton question;
	private Bitmap bmpBack01;
	private Bitmap bmpBack02;
	private Bitmap bmpQuestion01;
	private Bitmap bmpQuestion02;
	private Bitmap bmpCollectNotice;
	private boolean deblocking_one = false;
	private boolean deblocking_two = false;
	private boolean deblocking_three = false;
	private boolean deblocking_four = false;
	private boolean question_click = false;
	private boolean deblocking_five = false;
	
	public GameCollectView(Context context) {
		super(context);
		SharedPreferences sp = context.getSharedPreferences("gameRecord", Context.MODE_PRIVATE);
		int currentRecord = sp.getInt("modeone", 0);
		if(currentRecord == 14)
			deblocking_one  = true;
		currentRecord = sp.getInt("modetwo", 0);
		if(currentRecord == 14)
			deblocking_two = true;
		currentRecord = sp.getInt("modethree", 0);
		if(currentRecord == 14)
			deblocking_three = true;
		currentRecord = sp.getInt("modefour", 0);
		if(currentRecord == 14)
			deblocking_four = true;
		if(deblocking_one == true && deblocking_two == true 
				&& deblocking_three == true && deblocking_four == true)
			deblocking_five = true;//前四关都开启，才开启最终奖励
		deblocking_one = deblocking_two = deblocking_three = deblocking_four = true;
		deblocking_five = true;
	}

	@Override
	public void draw() {
		canvas.drawBitmap(background, null, bgRect,paint);
		back.draw(canvas, paint);
		question.draw(canvas, paint);
		if(deblocking_one)
			canvas.drawBitmap(bmpCollect_unlock.get(0), null,collectRect.get(0), paint);
		else 
			canvas.drawBitmap(bmpCollect_lock.get(0), null,collectRect.get(0), paint);
		
		if(deblocking_two)
			canvas.drawBitmap(bmpCollect_unlock.get(1), null,collectRect.get(1), paint);
		else 
			canvas.drawBitmap(bmpCollect_lock.get(1), null,collectRect.get(1), paint);
		
		if(deblocking_three)
			canvas.drawBitmap(bmpCollect_unlock.get(2), null,collectRect.get(2), paint);
		else 
			canvas.drawBitmap(bmpCollect_lock.get(2), null,collectRect.get(2), paint);
		
		if(deblocking_four)
			canvas.drawBitmap(bmpCollect_unlock.get(3), null,collectRect.get(3), paint);
		else 
			canvas.drawBitmap(bmpCollect_lock.get(3), null,collectRect.get(3), paint);
		
		if(deblocking_five)
			canvas.drawBitmap(bmpCollect_unlock.get(4), null,collectRect.get(4), paint);
		else 
			canvas.drawBitmap(bmpCollect_lock.get(4), null,collectRect.get(5), paint);
		
		if(question_click)
			canvas.drawBitmap(bmpCollectNotice, null, noticeRect, paint);
	}

	@Override
	public void logic() {
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_UP){
			if(question_click == false){
//				if(collectRect.get(0).contains(event.getX(),event.getY())){
//					if(deblocking_one){
//						Intent intent  = new Intent();
//						intent.setClass(context,GameWaveActivity.class);
//						intent.putExtra("id", 0);
//						context.startActivity(intent);
//						((GameCollectActivity)context).finish();
//						((GameCollectActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
//					}else{
//						question_click = true;
//					}
//				}
//				else if(collectRect.get(1).contains(event.getX(),event.getY())){
//					if(deblocking_two){
//						Intent intent  = new Intent();
//						intent.setClass(context,GameWaveActivity.class);
//						intent.putExtra("id", 1);
//						context.startActivity(intent);
//						((GameCollectActivity)context).finish();
//						((GameCollectActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
//					}else{
//						question_click = true;
//					}
//				}
//				else if(collectRect.get(2).contains(event.getX(),event.getY())){
//					if(deblocking_three){
//						Intent intent  = new Intent();
//						intent.setClass(context,GameWaveActivity.class);
//						intent.putExtra("id", 2);
//						context.startActivity(intent);
//						((GameCollectActivity)context).finish();
//						((GameCollectActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
//					}else{
//						question_click = true;
//					}
//				}
//				else if(collectRect.get(3).contains(event.getX(),event.getY())){
//					if(deblocking_four){
//						Intent intent  = new Intent();
//						intent.setClass(context,GameWaveActivity.class);
//						intent.putExtra("id", 3);
//						context.startActivity(intent);
//						((GameCollectActivity)context).finish();
//						((GameCollectActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
//					}else{
//						question_click = true;
//					}
//				}
				 if(collectRect.get(4).contains(event.getX(),event.getY())){
					if(deblocking_five){
						Intent intent  = new Intent();
						intent.setClass(context,GameWaveActivity.class);
						intent.putExtra("id", 4);
						context.startActivity(intent);
						((GameCollectActivity)context).finish();
						((GameCollectActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
					}else{
						question_click = true;
					}
				}
			}else{
				question_click = false;
			}
		}
		back.onTouchEvent(event);
		question.onTouchEvent(event);
		return true;
	}

	@Override
	public void initBitmap() {
		BitmapManager.getInstance(context).initCollectBitmap();
		background = BitmapManager.getInstance(context).bmpScreenCollect;
		bmpCollect_lock = BitmapManager.getInstance(context).bmpCollect_lock;
		bmpCollect_unlock = BitmapManager.getInstance(context).bmpCollect_unlock;
		bmpBack01 = BitmapManager.getInstance(context).bmpBack01;
		bmpBack02 = BitmapManager.getInstance(context).bmpBack02;
		bmpQuestion01 = BitmapManager.getInstance(context).bmpQuestion01;
		bmpQuestion02 = BitmapManager.getInstance(context).bmpQuestion02;
		back = new MyButton(bmpBack01,bmpBack02) {
			@Override
			public void doClick() {
				SoundManager.getInstance(context).playSoundClick();
				Intent intent = new Intent();
				intent.setClass(context, GameModeActivity.class);
				intent.putExtra("position", GameCollectActivity.index);
				context.startActivity(intent);
				((GameCollectActivity)context).finish();
				((GameCollectActivity)context).overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
			}
		};
		question = new MyButton(bmpQuestion01,bmpQuestion02) {
			@Override
			public void doClick() {
				if(question_click == false)
					question_click = true;
			}
		};
		bmpCollectNotice = BitmapManager.getInstance(context).bmpCollectNotice;
	}

	@Override
	public void initRect() {
		bgRect = new Rect(0,0,screenW,screenH);
		collectRect = new ArrayList<MyRect>();
		for(int i = 0; i<4; i++){
			MyRect level = new MyRect(i*screenW/4, screenH/20+screenH/10, screenW/4, screenH*5/12);
			collectRect.add(level);
		}
		MyRect level = new MyRect(screenW*3/16,screenH/20+screenH*5/12+screenH/10 ,  screenW*5/8, screenH*5/12);
		collectRect.add(level);
		MyRect level01 = new MyRect(screenW/2-screenW/8,screenH/20+screenH/10+screenH*5/12 ,  screenW/4, screenH*5/12);
		collectRect.add(level01);
		
		backRect = new MyRect(screenW/40, screenH/30, screenW/15,screenH/9);
		back.setRect(backRect);
		questionRect = new MyRect(screenW*39/40-screenW/15, screenH/30, screenW/15,screenH/9);
		question.setRect(questionRect);
		noticeRect = new MyRect(screenW/10, screenH/10, screenW*4/5, screenH*3/4);
	}

	@Override
	public void releaseBitmap() {
		BitmapManager.getInstance(context).releaseCollectBitmap();
	}

}
